package com.maxmpz.audioplayerTest;

import android.test.ActivityInstrumentationTestCase2;
import android.util.Log;

import com.jayway.android.robotium.solo.Solo;



@SuppressWarnings("unchecked")
public class TestAPK extends ActivityInstrumentationTestCase2 {
	private static final String TARGET_PACKAGE_ID = "com.maxmpz.audioplayer"; //CHANGE
	private static final String LAUNCHER_ACTIVITY_FULL_CLASSNAME = "com.maxmpz.audioplayer.StartupActivity"; //CHANGE
	private static Class launcherActivityClass;
	static {
		try {
			launcherActivityClass = Class.forName(LAUNCHER_ACTIVITY_FULL_CLASSNAME);
		} catch (ClassNotFoundException e) {
			throw new RuntimeException(e);
		}
	}

	public TestAPK() throws ClassNotFoundException {
		super(TARGET_PACKAGE_ID, launcherActivityClass);
	}

	private Solo solo;

	@Override
	protected void setUp() throws Exception {
		solo = new Solo(getInstrumentation(), getActivity());
	}
	
	public void testPlaySongWMA() {
		int folderOther = 15;
		solo.clickOnMenuItem("Folders/Lib");
		solo.clickOnButton("Folders");
		//solo.clickOnImageButton(1);
		solo.clickInList(folderOther);
		solo.clickInList(1);
		solo.sleep(2000);
		solo.clickOnImageButton(7);	//pause button
		solo.sleep(2000);
	}
	
	public void testPlaySongMP3() {
		int folderSum41 = 17;
		solo.clickOnMenuItem("Folders/Lib");
		solo.clickOnButton("Folders");
		solo.clickInList(folderSum41);
		solo.clickInList(5);
		solo.sleep(2000);
		solo.clickOnImageButton(7);	//pause button
		solo.sleep(2000);
	}

	public void testNextSong() {
		int folderSum41 = 17;
		solo.clickOnMenuItem("Folders/Lib");
		solo.clickOnButton("Folders");
		solo.clickInList(folderSum41);
		solo.clickInList(5);
		solo.sleep(2000);
		solo.clickOnImageButton(7);	//pause button
		solo.clickOnImageButton(8); // next song
		solo.sleep(2000);
		solo.clickOnImageButton(7); // pause
		solo.sleep(1000);
	}	
	
	public void testVideoPlayback() {	//TODO nicht mit 3gp - anderes noch nicht probiert
		solo.clickOnMenuItem("Folders/Lib");
		solo.clickOnButton("Folders");
		solo.clickInList(1);
		solo.clickInList(1);
		solo.sleep(2000);
		solo.sleep(3000);
	}
	
	public void testPlaylistCreate() {
		String playlist = "TestPlaylist";
		int folderSum41 = 17;
		solo.clickOnMenuItem("Folders/Lib");
		solo.clickOnButton("Folders");
		solo.clickOnMenuItem("Add to Playlist");
		solo.clickInList(folderSum41);
		solo.clickOnButton(2);
		solo.clickOnButton("Create new playlist");
		solo.clearEditText(0);		
		solo.enterText(0, playlist);
		solo.sleep(2000);
		solo.clickOnButton(3);
		solo.clickInList(5);	//playlists select
		solo.clickInList(3);	//new created playlist select
		solo.clickInList(1);
		solo.sleep(3000);
	}
	
	public void testEqualizer() {
		solo.clickOnMenuItem("Equalizer");
		solo.clickOnButton("Preset");
		solo.clickInList(7);	//dance preset
		assertTrue(solo.searchText("Dance"));
		solo.goBack();
		solo.clickOnImageButton(7);	//play
		solo.sleep(4000);
	}
	
	public void testTagsEdit() {
		int folderSum41 = 17;
		String testTag = "TestTag";
		solo.clickOnMenuItem("Folders/Lib");
		solo.clickOnButton("Folders");
		solo.clickInList(folderSum41);
		solo.clickInList(5);
		solo.sleep(2000);
		solo.clickOnMenuItem("More");
		solo.clickOnMenuItem("Info/Tags");
		solo.clickOnButton(1);
		solo.sleep(2000);
		solo.enterText(0, testTag);
		solo.clickOnButton(1);	//save
		solo.clickOnImageButton(7);	//play
		solo.sleep(4000);
	}

	public void testPlaySongMP3WithHeadsetPlugIn() {
		Log.d(getName(), "Please Plug in the Headset");
		solo.sleep(5000); // plug in headset
		int folderSum41 = 17;
		solo.clickOnMenuItem("Folders/Lib");
		solo.clickOnButton("Folders");
		solo.clickInList(folderSum41);
		solo.clickInList(5);
		solo.sleep(2000);
		solo.clickOnImageButton(7);	//pause button
		solo.sleep(2000);
	}
	
	@Override
	public void tearDown() throws Exception {
		solo.finishOpenedActivities();
	}
}